package com.useeinfo.oa.modules.maintaining.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.maintaining.entity.PlanExecuteTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 */
@Repository("planExecuteTimeDao")
public class PlanExecuteTimeDao implements CrudDao<PlanExecuteTime> {

    private final static Logger logger = LoggerFactory.getLogger(PlanExecuteTimeDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    public Integer setOperateStatus(int status,Long planId) {
        String queryString = "update PlanExecuteTime p set p.operateStatus= :status where  p.planerOrder.id = :planId";
        logger.info("拼接后得到查询语句为:[{}]", queryString);
        Query query = entityManager.createQuery(queryString);

        query.setParameter("status", status);
        query.setParameter("planId", planId);
        Integer result = query.executeUpdate();
        return result;
    }

    public void deleteTime(Long id) {
        entityManager.createQuery("delete from PlanExecuteTime p where p.plannerOrder.id ="+id).executeUpdate();
    }




    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        for (String queryKey : queryHash.keySet()) {
            String queryValue = queryHash.get(queryKey);
            switch (queryKey) {
                case "plannerId":
                    conditionHash.put("f.plannerOrder.id = ?{paramIndex} ", StringConverters.null2Long(queryValue));
                    continue;

                case "remindTime":
                    conditionHash.put("f.executeTime <= ?{paramIndex} ", StringConverters.ToDatePattern(queryValue, "yyyy-MM-dd HH:mm"));
                    continue;
                case "status":
                    conditionHash.put("f.status = ?{paramIndex} ", StringConverters.null2Int(queryValue));
                    continue;
                case "operateStatus":
                    conditionHash.put("f.operateStatus = ?{paramIndex} ", StringConverters.null2Int(queryValue));
                    continue;
            }

        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(f) from PlanExecuteTime f ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<PlanExecuteTime> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select f from  PlanExecuteTime f ", "order by f.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<PlanExecuteTime> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, PlanExecuteTime.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public PlanExecuteTime findModel(Long id) {
        return entityManager.find(PlanExecuteTime.class, id);
    }

    @Override
    public Integer add(PlanExecuteTime model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("PlanExecuteTimeDaoImpl添加PlanExecuteTime成功！");
        return 1;
    }

    @Override
    public Integer update(PlanExecuteTime model) {
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        PlanExecuteTime planExecuteTime = entityManager.find(PlanExecuteTime.class, id);
        entityManager.remove(planExecuteTime);
        return 1;
    }

    @Override
    public Integer deleteLogic(PlanExecuteTime model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
